package com.example.month11.appupdate.updater.ui;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.example.month11.appupdate.updater.AppUpdater;
import com.example.month11.appupdate.updater.bean.AppUtils;
import com.example.month11.appupdate.updater.bean.DownloadBean;
import com.example.month11.appupdate.updater.net.INetDownloadCallBack;
import com.example.recyclerview01.R;

import java.io.File;

// DialogFragment下载弹框
// DialogFragment创建Dialog的两种方式：onCreateView、onCreateDialog
public class UpdateVersionShowDialog extends DialogFragment {
    public static final String KEY_DOWNLOAD_BEAN = "download_bean";
    private DownloadBean mDownloadBean;
    private TextView tvUpdate;

    public static void show(FragmentActivity activity, DownloadBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_DOWNLOAD_BEAN, bean);
        UpdateVersionShowDialog dialog = new UpdateVersionShowDialog();
        dialog.setArguments(bundle); // 传递参数给Fragment。
        dialog.show(activity.getSupportFragmentManager(), "updateVersionShowDialog");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mDownloadBean = (DownloadBean) arguments.getSerializable(KEY_DOWNLOAD_BEAN);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_updatar, container, false);
        bindEvents(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void bindEvents(View view) {
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvDesc = view.findViewById(R.id.tv_desc);
        tvUpdate = view.findViewById(R.id.tv_update);

        tvTitle.setText(mDownloadBean.title);
        tvDesc.setText(mDownloadBean.content);
        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                // 下载操作。
                downLoad(v);
            }
        });
    }

    private void downLoad(View view) {
        // 下载操作。
        File targetFile = new File(getActivity().getCacheDir(), "target.apk");
        AppUpdater.getInstance().getNetManager().download(mDownloadBean.url, targetFile, new INetDownloadCallBack() {
            @Override
            public void success(File apkFile) {
                view.setEnabled(true);
                dismiss();
                String fileMd5 = AppUtils.getFileMd5(targetFile);
                if (fileMd5 != null && fileMd5.equals(mDownloadBean.md5)) {
                    // check md5
                    AppUtils.installApk(getActivity(), apkFile);
                } else {
                    // md5检测失败。保证文件的完整性。
                }
            }

            @Override
            public void progress(int progress) {
                tvUpdate.setText(progress + "% 已下载");
            }

            @Override
            public void failed(Throwable throwable) {
                view.setEnabled(true);
                //Toast 文件下载失败。
            }
        }, UpdateVersionShowDialog.this);
    }

    @Override
    public void onDismiss( DialogInterface dialog) {
        super.onDismiss(dialog);
        AppUpdater.getInstance().getNetManager().cancel(this);
    }
}
